﻿import flash.geom.Point;
import MyTesture.SimpleTestures;
dynamic class MyTesture.Testure extends MovieClip {
	private static var testureCounts:Number = 0;
	public static function get NewTestureIndex():Number {
		return testureCounts++;
	}

	private var $rootmc:MovieClip;
	private var $PicUrl:String;
	private var $PicLink:String;
	
	private var $points:Array;
	private var $testures:Array;
	private var $id:Number;
	
	public function get ID():Number {
		if ($id == undefined) {
			$id = NewTestureIndex;
		}
		return this.$id;
	}
	public function get Testures():Array {
		return this.$testures;
	}
	public function get Points():Array {
		return this.$points;
	}
	public function get PicUrl():String { 
		return $PicUrl; 
	}
	
	public function set PicUrl(value:String):Void {
		$PicUrl = value;
		for (var i = 0; i < this.$testures.length; i++) {
			SimpleTestures(this.$testures[i]).PicUrl = this.$PicUrl;
		}
	}
	
	public function get PicLink():String {
		return $PicLink;
	}
	
	public function set PicLink(value:String):Void {
		$PicLink = value;
		for (var i = 0; i < this.$testures.length; i++) {
			SimpleTestures(this.$testures[i]).PicLink = this.$PicLink;
		}
	}
	
	
	public function Testure(root:MovieClip) {
		if (root == undefined) {
			root = this;
		}
		this.$rootmc = root;
		initvars();
	}
	private function initvars() {
		this.$testures = new Array();
	}
	
	public function dotesture(points:Array):Object {
			var obj = { };
		if (points == undefined || points.length <= 0) {
			for (var i = 0; i < this.$testures.length; i++) {
				var t:MyTesture.SimpleTestures = SimpleTestures(this.$testures[i]);
				t.testure(true);
				obj[t.ID] = t;
			}
		}else {
			trace("??")
			for (var i = 0; i < points.length; i++) {
				var ar:Array = points[i].testures;
				for (var l = 0; l < ar.length; l++) {
					var t:MyTesture.SimpleTestures = SimpleTestures(ar[l]);
					if (obj[t.ID] == undefined) {
						obj[t.ID] = t;
						t.testure(true);
					}
				}
			}
			return obj;
		}
	}
	/**
	 * 创建贴图mc和点
	 * @param	points
	 */
	public static function createMcAndPoints(tmc:MovieClip, points:Array, picurl:String, piclink:String):MyTesture.Testure {
		if (tmc == undefined) {
			return null;
		}
		var wpic:Number = points.length;
		var hpic:Number = points[0].length;
		
		if (wpic == undefined || wpic < 2 || hpic == undefined || hpic < 2) {
			trace("贴图点数据不正确，至少为一组2元2维数组");
			return null;
		}
		var uselink = (picurl == "" || picurl == undefined || picurl == null);
		
		var t:MyTesture.Testure = new MyTesture.Testure(tmc);
		t.$points = points;
		t.PicUrl = picurl;
		t.PicLink = piclink;
		var stmc:MovieClip = tmc.createEmptyMovieClip("testure" + t.ID, t.ID + 1);
		
		var wUVoffset:Number = 100;
		var hUVoffset:Number = 100;
		for (var i = 0; i < wpic-1; i++) {
			for (var l = 0; l < hpic - 1; l++) {
				/**/
				var st1:MyTesture.SimpleTestures = new MyTesture.SimpleTestures(stmc);
				st1.Points = [t.$points[i + 1][l], t.$points[i][l], t.$points[i][l + 1]];
				st1.UVScale = new Point((hpic - 1) * 100, (wpic - 1) * 100);
				if (uselink) {
					st1.PicLink = t.PicLink;
				}else {
					st1.PicUrl = t.PicUrl;
				}
				st1.UV = new Point(l * wUVoffset, i * hUVoffset);
				st1.TestureType = "p";
				t.Testures.push(st1);
				
				var st2:MyTesture.SimpleTestures = new MyTesture.SimpleTestures(stmc);
				st2.Points = [t.$points[i][l + 1], t.$points[i + 1][l + 1], t.$points[i + 1][l]];
				st2.UVScale = new Point((hpic - 1) * 100, (wpic - 1) * 100);
				if (uselink) {
					st2.PicLink = t.PicLink;
				}else {
					st2.PicUrl = t.PicUrl;
				}
				st2.UV = new Point((l+1) * wUVoffset, (i+1) * hUVoffset);
				st2.TestureType = "d";
				t.Testures.push(st2);
				//Debug.trace(t.$points[0][0]);
			}
		}
		return t;
	}
}